package Client;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author antoinec
 */
public class Connection {

    private int numPort;
    private String nomServeur;
    private AttenteMessage attentemessage;
    private Socket socket;
    private String id;
    private Protocole protocole;
    private boolean etatAttente=false;

    public Connection (){
        this.protocole = new Protocole(this);
        this.nomServeur="transit.iut2.upmf-grenoble.fr";
        this.numPort=22222;
        
    }

    public boolean getEtat(){
        return this.etatAttente;
    }
    public void setEtat(boolean etat){
        this.etatAttente=etat;
    }

    public Socket ouvrirConnection(){
    	try {
			socket = new Socket(this.nomServeur, this.numPort);
            
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return socket;
    }
    
    public void fermerConnection(){
    	try {
            this.etatAttente =false;
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public void demarrerAttente(){
        this.attentemessage=new AttenteMessage(this);
        this.attentemessage.start();
    }


 	public Vector<String> listeConnectes(){
        Vector<String> liste = this.protocole.demanderListeConnectes();
        
		return liste;
	}

	public void envoyerMessage(String id, String message){
		this.protocole.envoitMessage(id, message);
	}
public void identification(String id,String mdp){
		this.protocole.identification(id,mdp);
	}

	public void inscription(String id,String mdp){
		this.protocole.inscription(id,mdp);
	}
	public Vector<String> recevoirMessage(){
		return this.protocole.recevoirMessage();
	}

    public void traiterRetour(){
        this.protocole.traiterCodeRetour();
    }


	
}